---
title: "Van Egeren et al (2018) Fig. 3"
output: html_notebook
---

```{r include=FALSE}
library(readr)
library(ggplot2)
library(reshape2)
library(RColorBrewer)
source("./utils.R")
```

## Fig. 3b

```{r include=FALSE}
time_dat = data.frame(A=numeric(0), B=numeric(0), C=numeric(0))
num_runs = 10000
filepath = "./data_time_long/"
max_time = 10000
max_time_interval = 1000

  for (j in c(1,5,10,20,50,100)){
    filename = paste(filepath, j, "/0.95/end_time.oevo", sep="")
    temp = read_csv(filename, col_names=FALSE)
    #print(max(temp$X2))
    ecdf_temp = ecdf(temp$X2)
    time_dat = rbind(time_dat, data.frame(A=(1:max_time)*max_time_interval, B=ecdf_temp((1:max_time)*max_time_interval), C=j))
  }

colnames(time_dat) = c("time", "cum_dist", "lifetimes")
```

```{r include=FALSE}
her_time = data.frame(A=numeric(0), B=numeric(0), C=numeric(0))
filename = paste(filepath, "heritable/0.95/end_time.oevo", sep="")
    temp = read_csv(filename, col_names=FALSE)
    ecdf_temp = ecdf(temp$X2)
    her_time = rbind(her_time, data.frame(A=(1:max_time)*max_time_interval, B=ecdf_temp((1:max_time)*max_time_interval), C=j))
    colnames(her_time) = c("time", "cum_dist", "lifetimes")
    
simple_time = data.frame(A=numeric(0), B=numeric(0), C=numeric(0))
filename = paste(filepath, "simple/0.95/end_time.oevo", sep="")
    temp = read_csv(filename, col_names=FALSE)
    ecdf_temp = ecdf(temp$X2)
    simple_time = rbind(simple_time, data.frame(A=(1:max_time)*max_time_interval, B=ecdf_temp((1:max_time)*max_time_interval), C=j))
    colnames(simple_time) = c("time", "cum_dist", "lifetimes")
```

```{r echo=FALSE}
#ggplot(data=time_dat) + geom_line(aes(x=time,y=cum_dist,color=sapply(lifetimes, to_colorscale) ,group=lifetimes)) + geom_line(data=her_time, aes(x=time,y=cum_dist), color="#0000cc", size=1) + geom_line(data=simple_time, aes(x=time,y=cum_dist), color="#707070", size=1) + theme_bw()+ theme(legend.position = "none", axis.title.x=element_blank(), axis.title.y=element_blank(), text = element_text(size=20), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_blank())  + scale_color_gradient2(low="#cccc00", mid="#ff3399", high="#9900ff", midpoint=4)

ggplot(data=time_dat) + geom_line(aes(x=time,y=cum_dist,color=sapply(lifetimes, to_colorscale) ,group=lifetimes)) + geom_line(data=her_time, aes(x=time,y=cum_dist), color="#4400cc", size=1) + geom_line(data=simple_time, aes(x=time,y=cum_dist), color="#707070", size=1) + theme_bw()+ theme(legend.position = "none", axis.title.x=element_blank(), axis.title.y=element_blank(), text = element_text(size=20), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_blank())  + scale_color_gradient2(low="#cccc00", mid="#ff3399", high="#9900ff", midpoint=4) + xlim(0, 10500000)
```

## Fig. 3c-d

```{r include=FALSE}
filepath = "./data_r1_regress/"
first = c(0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1)
second = c(1,5,10,20,50,100)
dat = extract_data(filepath, first, second, 10000)
```

```{r echo=FALSE}
plots = plot_fix_tunnel(dat$fix_dat, dat$simple_dat, dat$her_dat)
# Fig. 3c
plots$fix

# Fig. 3d
plots$tunnel
```
